package ro.msg.quiz.view;

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import ro.msg.quiz.controller.Controller;

/**
 * Create the panel.
 * 
 * @author Adorjan
 */
public class AddQuestionStepOnePanel extends JPanel {
	private static int NOT_ASSIGNED = -1;
	private static int ADD_MODE = 0;
	private static int EDIT_MODE = 1;
	private final String[] qTypes = { "Single choice", "Multiple choice",
			"Text" };
	private JComboBox comboBox = new JComboBox(qTypes);
	private final JLabel lblQuestion = new JLabel("Question");
	private final JLabel lblType = new JLabel("Type");
	private final JLabel lbAnswerNo = new JLabel("Nr. of possible answers");
	private final JTextField txtQuestion;
	private final JTextField txtAnswerNo = new JTextField();
	private int txtQuestionClicked = 0;
	private final Controller ctrl;
	private int state; // if == 0 --> add; else if == 1 --> edit
	private final int questionId;

	/**
	 * The panel's constructor
	 * 
	 * @author Adorjan
	 */
	public AddQuestionStepOnePanel(Controller controller, Integer question) {
		setLayout(null);
		this.ctrl = controller;
		this.questionId = question;

		/*
		 * if there is an existing question we are in edit mode
		 */
		if (questionId != NOT_ASSIGNED)
			state = EDIT_MODE;

		lblQuestion.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblQuestion.setBounds(22, 11, 49, 27);
		add(lblQuestion);

		txtQuestion = new JTextField();
		/**
		 * Title textfield's mouse listener
		 */
		txtQuestion.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (txtQuestionClicked == 0) {
					txtQuestion.setText(null);
					txtQuestionClicked = 1;
				}
			}
		});
		txtQuestion.setHorizontalAlignment(SwingConstants.CENTER);

		/*
		 * If add mode
		 */
		if (state == ADD_MODE) {
			txtQuestion.setText("Your text here");
			comboBox.setSelectedIndex(0);
		}
		/*
		 * If edit mode
		 */
		else {
			txtQuestion.setText(ctrl.getFromQuestionList(questionId).getText());
			txtQuestionClicked = 1;
			comboBox.setSelectedIndex(ctrl.getFromQuestionList(questionId)
					.getType() - 1);
			if (comboBox.getSelectedIndex() == 2) {
				txtAnswerNo.setEnabled(false);
			}
			// TODO should add the current number of answers
			// txtAnswerNo.setText();
		}

		txtQuestion.setFont(new Font("Tahoma", Font.PLAIN, 14));
		txtQuestion.setBounds(22, 49, 352, 73);
		add(txtQuestion);
		txtQuestion.setColumns(10);

		lblType.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblType.setBounds(22, 158, 28, 14);
		add(lblType);

		/**
		 * Combobox's item listener
		 */
		comboBox.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent arg0) {
				if (state == ADD_MODE)
					txtAnswerNo.setText(null);
				if (comboBox.getSelectedIndex() == 2) {
					txtAnswerNo.setEnabled(false);
				} else
					txtAnswerNo.setEnabled(true);

			}
		});
		comboBox.setBounds(71, 156, 183, 20);
		add(comboBox);

		lbAnswerNo.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lbAnswerNo.setBounds(22, 200, 127, 14);
		add(lbAnswerNo);

		txtAnswerNo.setBounds(168, 198, 86, 20);
		/**
		 * Answer number textfield's key listener
		 */
		txtAnswerNo.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char caracter = e.getKeyChar();
				if (((caracter < '0') || (caracter > '9'))) {
					e.consume();
				}
			}
		});
		add(txtAnswerNo);
		txtAnswerNo.setColumns(1);

	}

	/**
	 * @return the txtQuestion
	 */
	public JTextField getTxtQuestion() {
		return txtQuestion;
	}

	/**
	 * @return the txtAnswerNo
	 */
	public JTextField getTxtAnswerNo() {
		return txtAnswerNo;
	}

	/**
	 * @return the comboBox
	 */
	public JComboBox getComboBox() {
		return comboBox;
	}

	/**
	 * 
	 * @param comboBox
	 *            the comboBox to set
	 */
	public void setComboBox(JComboBox comboBox) {
		this.comboBox = comboBox;
	}

}
